package com.example.onlinetaxi.ui.activity.CommentPage;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.IExchangeFragments;
import com.example.onlinetaxi.dto.CommentDto.CommentDto;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.ui.activity.BaseActivity;
import com.example.onlinetaxi.ui.activity.CommentPage.DriverCommentFragments.DriverCommentFragment;
import com.example.onlinetaxi.ui.activity.CommentPage.PassengerCommentFragments.PassengerCommentFragment;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.util.TimeUtil;
import com.example.onlinetaxi.view.Dialog.LoadingDialog;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

// 评价司机界面
public class CommentActivity extends BaseActivity {

    // 评论不可见fragment
    private CannotCommentFragment passengerCannotComment = new CannotCommentFragment("请支付该订单后再进行评价~");
    private CannotCommentFragment driverCannotComment = new CannotCommentFragment("您提交评价后才能查看司机评价哟~");
    private CannotCommentFragment passengerCannotComment2 = new CannotCommentFragment("乘客还没有做出评价哟~");
    private CannotCommentFragment driverCannotComment2 = new CannotCommentFragment("司机还没有做出评价哟~");
    // 乘客评价
    private PassengerCommentFragment passengerCommentFragment = new PassengerCommentFragment();
    // 司机评价
    private DriverCommentFragment driverCommentFragment = new DriverCommentFragment();
    // 显示乘客评价
    private ShowCommentFragment showPassengerCommentFragment = new ShowCommentFragment();
    // 显示司机评价
    private ShowCommentFragment showDriverCommentFragment = new ShowCommentFragment();
    // 组件
    private TextView commentOrderId;
    private TextView commentOrderLeftColor;
    private TextView commentOrderRightInfo;
    private TextView commentOrderUseCarTime;
    private TextView commentOrderUseCarType;
    private TextView commentOrderStartInfo;
    private TextView commentOrderEndInfo;
    private TextView commentOrderCarBrand;
    private TextView commentOrderCarColor;
    private TextView commentOrderCarNumber;
    private TextView commentOrderGetPickUpTime;
    private TextView commentOrderArriveTime;
    private TextView commentOrderCost;
    // 返回按钮
    private ImageView back;

    // 订单实体
    private CommentDto passengerComment;
    private CommentDto driverComment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);

        // 根据LocalStorage.chosenOrder来初始化界面
        init();

        // 关闭加载框
        LoadingDialog.stopLoading();
    }

    private void init() {
        initView();

        initEvent();
    }

    private void getComments() {
        // 通过订单号向后台获取所有该订单的评论
        Map<String, String> map = new HashMap<>();
        map.put("orderId", LocalStorage.chosenOrder.getCompleteOrderId().toString());

        String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "comment", "getComments");
        DataResponse dataResponse = GetDataResponse.getDataResponse(response);

        if (dataResponse.getCode().equals("200")) {
            Gson gson = new Gson();

            if (dataResponse.getData().containsKey("passenger")) {
                passengerComment = gson.fromJson(dataResponse.getData().get("passenger").toString(), CommentDto.class);
            }
            if (dataResponse.getData().containsKey("driver")) {
                driverComment = gson.fromJson(dataResponse.getData().get("driver").toString(), CommentDto.class);
            }
        }
    }

    // 可以用来刷新页面
    public void initEvent() {
        // 尝试获取该订单的所有评价
        getComments();

        // 设置fragment
        // 判断订单的缴费情况
        if (LocalStorage.type.equals("driver")) {
            // 司机评价页面
            if (passengerComment != null) {
                // 乘客已评价
                showPassengerCommentFragment.setData(passengerComment);
                replacePassengerFragment(showPassengerCommentFragment);
            } else {
                // 乘客为评价
                replacePassengerFragment(passengerCannotComment2);
            }
            if (driverComment != null) {
                // 司机已评价
                showDriverCommentFragment.setData(driverComment);
                replaceDriverFragment(showDriverCommentFragment);
            } else {
                // 司机未评价
                replaceDriverFragment(driverCommentFragment);
            }

        } else if (LocalStorage.type.equals("passenger")) {
            // 乘客评价页面
            if (passengerComment != null) {
                // 1.用户已经评论

                // 显示用户评价
                showPassengerCommentFragment.setData(passengerComment);
                replacePassengerFragment(showPassengerCommentFragment);

                // 判断司机评价
                if (driverComment == null) {
                    // 司机未评价
                    replaceDriverFragment(driverCannotComment2);
                } else {
                    // 司机已经评价，显示司机评价
                    showDriverCommentFragment.setData(driverComment);
                    replaceDriverFragment(showDriverCommentFragment);
                }

            } else if (LocalStorage.chosenOrder.getState().equals("7")) {
                // 2.用户未支付

                // 用户不可评价
                replacePassengerFragment(passengerCannotComment);
                // 用户不可见司机评价
                replaceDriverFragment(driverCannotComment);

            } else if (LocalStorage.chosenOrder.getState().equals("8")) {
                // 3.用户已支付

                // 用户可以评价
                replacePassengerFragment(passengerCommentFragment);
                // 用户评价前看不到司机评价
                replaceDriverFragment(driverCannotComment);

            }

            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 关闭当前页面
                    finish();
                }
            });

        }
    }

    private void initView() {
        // 获取组件
        commentOrderId = findViewById(R.id.comment_order_id);
        commentOrderUseCarTime = findViewById(R.id.comment_order_use_car_time);
        commentOrderUseCarType = findViewById(R.id.comment_order_use_car_type);
        commentOrderStartInfo = findViewById(R.id.comment_order_start_info);
        commentOrderEndInfo = findViewById(R.id.comment_order_end_info);
        commentOrderCarBrand = findViewById(R.id.comment_order_car_brand);
        commentOrderCarColor = findViewById(R.id.comment_order_car_color);
        commentOrderCarNumber = findViewById(R.id.comment_order_car_number);
        commentOrderGetPickUpTime = findViewById(R.id.comment_order_pick_up_time);
        commentOrderArriveTime = findViewById(R.id.comment_order_arrived_time);
        commentOrderCost = findViewById(R.id.comment_order_cost);
        back = findViewById(R.id.comment_back_btn);
        // 可变色
        commentOrderLeftColor = findViewById(R.id.comment_order_left_color);
        commentOrderRightInfo = findViewById(R.id.comment_order_rightInfo);

        // 设置组件
        commentOrderId.setText("当前订单id:" + LocalStorage.chosenOrder.getId());
        commentOrderUseCarTime.setText("用车时间:" + TimeUtil.DateToString(LocalStorage.chosenOrder.getUseCarTime()));
        commentOrderUseCarType.setText(LocalStorage.chosenOrder.getUseCarType());
        commentOrderStartInfo.setText(LocalStorage.chosenOrder.getStartInfo());
        commentOrderEndInfo.setText(LocalStorage.chosenOrder.getEndInfo());
        commentOrderCarBrand.setText("车辆品牌：" + LocalStorage.chosenOrder.getCarBrand());
        commentOrderCarColor.setText("车辆颜色：" + LocalStorage.chosenOrder.getCarColor());
        commentOrderCarNumber.setText("车牌号:" + LocalStorage.chosenOrder.getCarNumber());
        if (LocalStorage.chosenOrder.getPickUpTime() != null) {
            commentOrderGetPickUpTime.setText("上车时间: " + TimeUtil.DateToString(LocalStorage.chosenOrder.getPickUpTime()));
        }
        if (LocalStorage.chosenOrder.getArriveTime() != null) {
            commentOrderArriveTime.setText("抵达时间: " + TimeUtil.DateToString(LocalStorage.chosenOrder.getArriveTime()));
        }

        commentOrderCost.setText("￥ 乘车费用" + LocalStorage.chosenOrder.getCost() + "元");

        if (LocalStorage.chosenOrder.getState().equals("7")) {
            // 待支付
            commentOrderLeftColor.setBackground(getDrawable(R.color.blue));
            commentOrderRightInfo.setTextColor(Color.parseColor("#8EA2F6"));
            commentOrderRightInfo.setText("待支付");
        } else if (LocalStorage.chosenOrder.getState().equals("8")) {
            // 已完成
            commentOrderLeftColor.setBackground(getDrawable(R.color.perfect_green));
            commentOrderRightInfo.setTextColor(Color.parseColor("#4BE4B0"));
            commentOrderRightInfo.setText("已完成");
        }

        passengerCommentFragment.setiExchangeFragments(new IExchangeFragments() {
            @Override
            public void exchangeFragment() {
                // 进行界面刷新
                initEvent();
            }
        });

        driverCommentFragment.setiExchangeFragments(new IExchangeFragments() {
            @Override
            public void exchangeFragment() {
                // 进行界面刷新
                initEvent();
            }
        });
    }

    // 进行碎片之间的切换
    private void replacePassengerFragment(Fragment fragment) {
        // 获取碎片管理类
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启碎片事物
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 创建事务
        fragmentTransaction.replace(R.id.comment_passenger_fragments, fragment);
        // 提交事务
        fragmentTransaction.commit();
    }

    // 进行碎片之间的切换
    private void replaceDriverFragment(Fragment fragment) {
        // 获取碎片管理类
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启碎片事物
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 创建事务
        fragmentTransaction.replace(R.id.comment_driver_fragments, fragment);
        // 提交事务
        fragmentTransaction.commit();
    }
}